﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TrainingManagerSystem.WebUI.TrainingManagerSystemService;

namespace TrainingManagerSystem.WebUI.Models
{
    public static class CourseExtensionHelper
    {
        public static MvcHtmlString RenderTableConfigurationCoursePopup(this HtmlHelper htmlHelper)
        {
            return new MvcHtmlString("<div class='acTitle title-view'>" +
                                     "Selected Fields</div>" +
                                     "<table id='selectedFileds'>" +
                                     "<tr>" +
                                     "<td>" +
                                     "<input type='checkbox' id='selectAll'/> " +
                                     "Select all</td></tr>" +
                                     "<tr><td>" +
                                     "<input type='checkbox' checked='checked' class='fields'/> Course Id</td><td>" +
                                     "<input type='checkbox' checked='checked' class='fields'/> Course Name</td><td>" +
                                     "<input type='checkbox' checked='checked' class='fields'/> Course CategoryId</td><td>" +
                                     "<input type='checkbox' checked='checked' class='fields'/> Course Category</td></tr><tr><td>" +
                                     "<input type='checkbox' checked='checked' class='fields'/> Course Category Name</td><td>" +
                                     "<input type='checkbox' checked='checked' class='fields'/> Course Year</td><td>" +
                                     "<input type='checkbox' checked='checked' class='fields'/> Costs</td><td>" +
                                     "<input type='checkbox' class='fields'/> Start Date</td></tr><tr><td>" +
                                     "<input type='checkbox' class='fields'/> End Date</td><td>" +
                                     "<input type='checkbox' class='fields'/> Currency</td><td>" +
                                     "<input type='checkbox' class='fields'/> Time Commit</td><td>" +
                                     "<input type='checkbox' class='fields'/> Trained Duration</td></tr><tr><td>" +
                                     "<input type='checkbox' class='fields'/> Note</td><td>" +
                                     "<div class='btns'>" + "<input type='button' value='Cancel' id='cancelBtn'/><input type='button' value='Accept' id='changeFields'/></div>");
        }

        #region Extension
        public static IEnumerable<CompositeThing> Childs(int id, List<CourseCategory> list, ref int i)
        {
            var result = new List<CompositeThing>();
            foreach (var courseCategory in list.Where(courseCategory => courseCategory.ParentCourseCategoryId == id))
            {
                var obj = new CompositeThing
                {
                    Name = courseCategory.Category + '_' + courseCategory.CourseCategoryId,
                    Children = new HashSet<CompositeThing>(Childs(courseCategory.CourseCategoryId, list, ref i)),
                    Id = courseCategory.CourseCategoryId
                };

                result.Add(obj);
                foreach (var c in obj.Children)
                {
                    c.Parent = obj;
                }
                i++;
            }
            return result;
        }
        public static List<CompositeThing> GetDataForTreeView(List<CourseCategory> list)
        {
            var result = new List<CompositeThing>();
            var n = list.Count;
            var i = 0;
            while (i < n)
            {
                var obj = new CompositeThing
                {
                    Id = list[i].CourseCategoryId,
                    Name = list[i].Category + '_' + list[i].CourseCategoryId,
                    Children = new HashSet<CompositeThing>(Childs(list[i].CourseCategoryId, list, ref i))
                };
                result.Add(obj);
                foreach (var tt in obj.Children)
                {
                    tt.Parent = obj;
                }
                i++;
            }
            return result;
        }

        #endregion
    }
}